package com.spark.lola.controller.ser;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.lola.logistics.constant.LogisticsConstant;
import com.spark.lola.logistics.entity.LocationInfo;
import com.spark.lola.logistics.service.LocationInfoService;
import com.spark.lola.service.ser.SupervisorService;
import com.spark.lola.support.common.constant.CommonConstant;
import com.spark.lola.support.common.entity.ShiroUser;
import com.spark.lola.support.common.util.ResponseBean;
import com.spark.lola.support.core.vo.SelectMap;
import com.spark.lola.support.wms.cache.CollectionCache;
import com.spark.lola.support.wms.configuration.WmsProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Zhi-peng wang
 * @date: 2020/07/03 18:24
 **/
@RestController
@RequestMapping("/api/service/index")
public class SupervisorController {

    @Autowired
    private SupervisorService supervisorService;

    @Autowired
    private LocationInfoService locationInfoService;

    @Autowired
    private WmsProperties wmsProperties;

    @RequestMapping(value = "/countNumber", method = RequestMethod.POST)
    public ResponseBean countNumber(@SessionAttribute ShiroUser shiroUser) {
        return ResponseBean.success(supervisorService.countNumber(shiroUser));
    }

    @RequestMapping(value = "/transactionNumber", method = RequestMethod.POST)
    public ResponseBean transactionNumber(@SessionAttribute ShiroUser shiroUser) {
        return ResponseBean.success(supervisorService.transactionNumber(shiroUser));
    }

    @RequestMapping(value = "/findMap", method = RequestMethod.POST)
    public ResponseBean findMap(@RequestBody List<JSONObject> mapList) {
        QueryWrapper<LocationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("area");
        queryWrapper.eq("location_type", LogisticsConstant.LocationType.PAPER);
        queryWrapper.eq("used", CommonConstant.Y);
        List<LocationInfo> locationInfoList = locationInfoService.list(queryWrapper);
        mapList.forEach(item -> {
            item.put("value", locationInfoList.stream().filter(location -> location.getArea().contains(item.getString("name"))).count());
        });
        return ResponseBean.success(mapList);
    }

    @RequestMapping(value = "/findMapInfo/{area}", method = RequestMethod.POST)
    public ResponseBean findMapInfo(@PathVariable String area) {
        QueryWrapper<LocationInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("location_type", LogisticsConstant.LocationType.PAPER);
        queryWrapper.eq("used", CommonConstant.Y);
        List<LocationInfo> locationInfoList = locationInfoService.list(queryWrapper);
        List<LocationInfo> list = locationInfoList.stream()
                .filter(location -> location.getArea().contains(area)).collect(Collectors.toList());
        return ResponseBean.success(list);
    }

    @RequestMapping(value = "/findInfoList", method = RequestMethod.POST)
    public ResponseBean finList(@SessionAttribute ShiroUser shiroUser) {
        return ResponseBean.success(supervisorService.finList());
    }

    @RequestMapping(value = "/findServiceList", method = RequestMethod.GET)
    public ResponseBean findServiceList(@SessionAttribute ShiroUser shiroUser) {
        List<ShiroUser> list = CollectionCache.dutyEmployees.get(wmsProperties.getService().getDutyId().get(1));
        return ResponseBean.success(list.stream().map(item -> new SelectMap(item.getId(), item.getRealName())).collect(Collectors.toList()));
    }

    @RequestMapping(value = "/findPurList", method = RequestMethod.GET)
    public ResponseBean findPurList(@SessionAttribute ShiroUser shiroUser) {
        List<ShiroUser> list = CollectionCache.dutyEmployees.get(wmsProperties.getPurchase().getDutyId().get(1));
        return ResponseBean.success(list.stream().map(item -> new SelectMap(item.getId(), item.getRealName())).collect(Collectors.toList()));
    }

}
